package main

import (
	"auth-service/internal/global"
	"auth-service/internal/ini"
	"auth-service/internal/router"
	"github.com/danfwing/sd-project/pkg/config"
	"github.com/danfwing/sd-project/pkg/middleware"
	"github.com/danfwing/sd-project/pkg/server"
	"github.com/gin-gonic/gin"
)

func main() {
	// 加载配置
	cfg, err := config.InitConfig()
	if err != nil || cfg == nil {
		panic(err)
	}
	global.GConfig = cfg
	// init
	err = ini.Init(cfg)
	if err != nil {
		panic(err)
	}
	// server init
	whiteList := middleware.NewWhiteListConfig()

	// 添加该服务特定的白名单路径
	whiteList.Add(
		"/api/auth/login",
		"/api/auth/register",
		"/api/refresh",
		"/swagger/*",
	)
	engine := server.NewServer(cfg, []gin.HandlerFunc{gin.Logger(), middleware.Cors(), middleware.AuthMiddleware(1, whiteList)})
	if engine == nil {
		panic("server init error")
	}
	router.InitRouter(engine)
	err = engine.Run()
	if err != nil {
		panic(err)
	}
}
